package com.itbbfx.service.system.impl;

import com.itbbfx.config.UserCredentialsMatcher;
import com.itbbfx.entity.*;
import com.itbbfx.mapper.RoleMapper;
import com.itbbfx.mapper.UserMapper;
import com.itbbfx.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public int queryUserName(String loginname) {
        return userMapper.queryUserName(loginname);
    }

    @Override
    public User queryLoginUser(String loginname) {
        return userMapper.queryLoginUser(loginname);
    }

    @Override
    public List<User> selectAllUser(User user) {
        return userMapper.selectAllUser(user);
    }

    @Override
    public String deleteUser(int userid) {
        if(userMapper.deleteUser(userid) > 0){
            if(userMapper.deleteUserRole(userid)>0){
                return "删除成功！";
            }
        }

        return "删除失败！";
    }

    @Override
    public int resetUserPwd(int userid) {
        String salt = UserCredentialsMatcher.generateSalt(6);
        String pwd = UserCredentialsMatcher.encryptPassword("md5","123456",salt,2);
        return userMapper.resetUserPwd(userid,pwd,salt);
    }

    @Override
    public String addUser(User user) {
        String salt = UserCredentialsMatcher.generateSalt(6);
        user.setPwd(UserCredentialsMatcher.encryptPassword("md5","123456",salt,2));
        user.setType(2);
        user.setSalt(salt);

        if(userMapper.addUser(user) > 0) {
            return "用户添加成功！";
        }

        return "用户添加失败！";
    }

    @Override
    public String updateUser(User user) {
        if(userMapper.updateUser(user) > 0) return "用户修改成功！";
        return "用户修改失败！";
    }

    @Override
    public List<Menu> queryUserRoleAuth(Integer roleid) {
        return userMapper.queryUserRoleAuth(roleid);
    }

    @Override
    public int saveUserRole(UserRole userRole) {
        if(userMapper.queryUserRole(userRole.getUserid()) > 0){
            return userMapper.deleteUserRole(userRole.getUserid());
        }

        return 0;
    }

    @Override
    public DataGridView initUserRole(int userid) {
        List<Role> roles = roleMapper.initUserRole();
        List<Integer> roleIds = userMapper.initUserRole(userid);

        List<Map<String, Object>> rs = new ArrayList<>();
        for (Role role : roles) {
            boolean LAY_CHECKED= false;

            for (Integer roleid : roleIds){
                if(roleid == role.getRoleid()){
                    LAY_CHECKED = true;
                    break;
                }
            }

            Map<String, Object> map = new HashMap<>();
            map.put("roleid",role.getRoleid());
            map.put("rolename",role.getRolename());
            map.put("roledesc",role.getRoledesc());
            map.put("LAY_CHECKED",LAY_CHECKED);
            rs.add(map);
        }

        return new DataGridView(rs);
    }

    @Override
    public int queryUserIdentity(String identity) {
        return userMapper.queryUserIdentity(identity);
    }

    @Override
    public User queryUser(Integer userid) {
        return userMapper.queryUser(userid);
    }

    @Override
    public int updateLoginUser(User user) {
        return userMapper.updateLoginUser(user);
    }

    @Override
    public int editPwd(User user) {
        return userMapper.editPwd(user);
    }
}